<?php
namespace app\api\controller\v1;
use app\admin\controller\Reason;
use app\api\logic\GetAddressLogic;
use app\api\logic\HuiLogic;
use app\api\logic\QuanLogic;
use app\api\logic\SendMuLogic;
use app\service\GaodeService;
use app\service\MoneyLogService;
use app\service\OrderService;
use think\Db;
use app\service\UserWeixinService;
use app\service\TechnWeixinService;
use app\service\PosterService;
use app\service\TechnService;
use app\admin\logic\ZzLogic;
use app\service\PaymentService;
use think\Log;
use think\Request;


class Huifubao extends Base
{
    public function _initialize()
    {
        parent::_initialize();
    }


    /**
     * 订单支付
     */
    public function pay()
    {
        $result['code'] = -1;
        $result['msg'] = "非法请求";
        $params = $this->data_post;
        $params['user'] = $this->user;
        $validate = $this->validate(
            $params,
            [
                'order_id' => 'require'
            ],
            [
                'order_id.require' => '订单id有误'
            ]
        );
        if(true !== $validate)
        {
            echo json_encode(['code' => -1, 'msg' => $validate]);
            exit();
        }
        //查询订单信息
        $order_info = Db::name('order')->where(['id' => $params['order_id']])->find();
        if(empty($order_info))
        {
            echo json_encode(['code' => -1, 'msg' => '订单不存在']);
            exit();
        }
        if($order_info['status'] != 0)
        {
            echo json_encode(['code' => -1, 'msg' => '订单不可支付']);
            exit();
        }
        //查询技师
        $techn = Db::name('techn')->where(['id' => $order_info['techn_id']])->find();
        if(empty($techn))
        {
            echo json_encode(['code' => -1, 'msg' => '技师不存在']);
            exit();
        }
        //判断技师是否在接单状态
        if($techn['work_status'] != 1)
        {
            echo json_encode(['code' => -1, 'msg' => '技师不在工作状态']);
            exit();
        }
//        //判断预约时间是否大于当前时间
        $pre_start_time = $order_info['pre_start_time'];
        if((time()+900) >= $pre_start_time)
        {
            echo json_encode(['code' => -1, 'msg' => '超过预约时间']);
            exit();
        }



        //技师预约时间节点
        $reservation_node = [];
        //查询技师已预约的订单
        $order_list = Db::name('order')->where(['techn_id' => $techn['id'], 'id' => ['neq', $order_info['id']], 'status' => ['in', '0,1,2,3,4,5'], 'order_model' => 0])->select();
        if(!empty($order_list))
        {
            foreach($order_list as $key => $value)
            {
                if(!empty($value['pre_start_time']) && !empty($value['pre_end_time']))
                {
                    //处理时间节点
                    $new_pre_start_time = $value['pre_start_time'];
                    $new_pre_end_time = $value['pre_end_time']+900;
                    for($j = $new_pre_start_time; $j <= $new_pre_end_time; $j += 1800)
                    {
                        $reservation_node[] = date('Y-m-d H:i:s', $j);
                    }
                }
            }
        }
        $techn['no_pre_hour_slot'] = !empty($techn['no_pre_hour_slot']) ? json_decode($techn['no_pre_hour_slot'], true) : [];
        //判断是否在技师工作时间
        //判断是否选择接单开始时间与结束时间
        $work_start_hour = !empty($techn['work_start_hour']) ? strtotime(date('Y-m-d', $pre_start_time).' '.$techn['work_start_hour'].':00') : strtotime(date('Y-m-d', $pre_start_time).' 00:00:00');
        $work_end_hour = !empty($techn['work_end_hour']) ? strtotime(date('Y-m-d', $pre_start_time).' '.$techn['work_end_hour'].':00') : strtotime(date('Y-m-d', $pre_start_time).' 23:30:00');
        if($work_start_hour > $work_end_hour)
        {
            if(!(($pre_start_time >= $work_start_hour || $pre_start_time <= $work_end_hour) && !in_array(date('Y-m-d H:i:s', $pre_start_time), $techn['no_pre_hour_slot']) && !in_array(date('Y-m-d H:i:s', $pre_start_time), $reservation_node)))
            {
                echo json_encode(['code' => -1, 'msg' => '不在技师工作时间内']);
                exit();
            }
        }elseif ($work_start_hour < $work_end_hour) {
            if(!($pre_start_time >= $work_start_hour && $pre_start_time <= $work_end_hour && !in_array(date('Y-m-d H:i:s', $pre_start_time), $techn['no_pre_hour_slot']) && !in_array(date('Y-m-d H:i:s', $pre_start_time), $reservation_node)))
            {
                echo json_encode(['code' => -1, 'msg' => '不在技师工作时间内']);
                exit();
            }
        }else
        {
            if(!(!in_array(date('Y-m-d H:i:s', $pre_start_time), $techn['no_pre_hour_slot']) && !in_array(date('Y-m-d H:i:s', $pre_start_time), $reservation_node)))
            {
                echo json_encode(['code' => -1, 'msg' => '不在技师工作时间内']);
                exit();
            }
        }

        //调用汇付宝支付接口
//        $pay_data = [
//            'user' => $params['user'],
//            'order_id' => $order_info['id'],
//            'order_no' => $order_info['order_no'],
//            'name' => '订单支付',
//            'total_price' => $order_info['total_price'],
//            'client_type' => $order_info['client_type'],
//            'notify_url' => request()->domain().'/api/v1/ordernotify/'.strtolower($payment['payment']),
//            'call_back_url' => request()->domain().'/h5/pages/order/order',
//            'redirect_url' => '',
//            'site_name' => '摩小妹',
//            'ajax_url' => '',
//        ];
        $order_detail = Db::name('order_detail')->where('order_id',$order_info['id'])->find();
        $pay_data = [
            'order_id' => $order_info['id'],
            'agent_bill_id' => $order_info['order_no'],
            'pay_amt' => $order_info['total_price'],
            'notify_url' => request()->domain().'/api/v1/Huifubao/notify_pay?order_no='.$order_info['order_no'],
            'return_url' => request()->domain().'/h5/pages/order/order',
            'goods_name' => $order_detail['title'],
            'goods_num' => $order_detail['buy_number'],
        ];

//        $pay_data = [
//            'order_id' => '24627',
//            'agent_bill_id' => '20231021152547773795',
//            'pay_amt' => '214.00',
//            'notify_url' => 'http://git_mo.com/api/v1/Huifubao/notify_pay?order_no=20231021152547773795',
//            'return_url' => 'http://git_mo.com/h5/pages/order/order',
//            'goods_name' => '中式推拿',
//            'goods_num' => 1,
//        ];

        $return = HuiLogic::pay($pay_data);
        if (!empty($return)){
            $result = ['code'=>0,'msg'=>'ok','data'=>['url'=>$return]];
        }else{
            $result = ['code'=>-1,'msg'=>'请重试'];

        }

        echo json_encode($result);
    }

    public function notify_pay(Request $request){
        $param = $request->param();
        $id = '2154762';

        //判断一下是否支付成功
        $result = HuiLogic::pay_backt($id,$param['order_no']);
        dump($result);
        //处理数据


    }




}